package com.junqson.je.content.service;

import com.alibaba.fastjson.JSON;
import com.junqson.je.common.util.AppUtil;
import com.junqson.je.content.model.ProblemModel;
import com.junqson.je.content.model.ProblemOption;
import com.junqson.je.content.model.ProblemSetModel;
import com.junqson.je.content.model.dto.ProblemDTO;
import com.junqson.je.content.model.enums.ProblemType;
import com.junqson.je.content.repo.ProblemRepo;
import com.junqson.je.content.repo.ProblemSetRepo;
import com.junqson.je.content.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Descriptions
 * @Company
 * @Author Junqson
 * @Date 2020/2/11 23:16
 * @Version 1.0
 */
@Service
@Slf4j
public class ProblemService {

    @Autowired
    ProblemSetRepo problemSetRepo;

    @Autowired
    ProblemRepo problemRepo;

    // 创建试题集
    public ProblemSetModel createProblemSet(ProblemSetModel model) {
        return problemSetRepo.save(model);
    }

    // 创建试题
    public void createProblem(ProblemDTO dto) {
        ProblemModel model = convertToProblemModel(dto);
        problemRepo.save(model);
    }

    // 删除试题集
    @Transactional
    public void deletePs(Long psId) {
        problemSetRepo.deleteById(psId);
        problemRepo.deleteByPsId(psId);
    }

    // 删除试题
    public void deleteProblem(Long problemId) {
        problemRepo.deleteById(problemId);
    }

    // 获取试题集列表
    public List<ProblemSetModel> pageProblemSet(Integer page, Integer size) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageUtil.getPageable(page, size, sort);
        return problemSetRepo.findAll(pageable).getContent();
    }

    // 获取试题集全部试题
    public List<ProblemDTO> getAllProblems(Long psId) {
        return problemRepo.findByPsIdOrderByTypeAsc(psId).stream().map(this::convertToProblemDTO).collect(Collectors.toList());
    }

    public ProblemDTO convertToProblemDTO(ProblemModel model) {
        ProblemDTO dto = new ProblemDTO();
        AppUtil.copyPropertiesIgnoreNull(model, dto);
        ProblemType type = ProblemType.values()[model.getType()];
        dto.setType(type.name());
        if (type == ProblemType.选择题) {
            List<ProblemOption> options = JSON.parseArray(model.getOptions(), ProblemOption.class);
            dto.setOptions(options);
        }
        return dto;
    }

    public ProblemModel convertToProblemModel(ProblemDTO dto) {
        ProblemModel model = new ProblemModel();
        AppUtil.copyPropertiesIgnoreNull(dto, model);
        model.setType(ProblemType.valueOf(dto.getType()).ordinal());
        if (ProblemType.valueOf(dto.getType()) == ProblemType.选择题) {
            String options = JSON.toJSONString(dto.getOptions());
            model.setOptions(options);
        }
        return model;
    }
}
